import sys

import requests
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText  # 往邮件中写内容的对象

from email.utils import formataddr  # 发件人信息
import sqlite3
# 目标网址
import time
import random
import string
import sqlite3
from datetime import datetime


# 创建数据库连接
conn = sqlite3.connect('E:\sqlite\GooglePlayAppsLink.db')
cursor = conn.cursor()
# 创建一个Cursor对象，并使用它执行SQL语句
# cursor = conn.cursor()
# # 关闭Cursor对象
# cursor.close()
# # 关闭数据库连接
# conn.close()

# msg_from = 'aleanchan@foxmail.com'
# password = 'ipanktlpktaeffie'

msg_from = 'aleangmx@foxmail.com'
password = 'hyfcoojcmkmndghh'

# msg_from = 'easyzaki@foxmail.com'
# password = 'ymkwowhoxevdbdbb'


# 注意：爬取过程可能受到robots.txt的限制，以及需要处理网页内容的加载和Ajax动态内容的抓取等复杂情况
def getMail(url):
    # 发送HTTP请求
    response = requests.get(url)
    # 确保网页请求成功
    if response.status_code == 200:
        # 使用BeautifulSoup解析网页内容
        soup = BeautifulSoup(response.text, 'html.parser')
        emaili = soup.find('i', string='email')
        details = emaili.parent.find_all('div', class_='pSEeg')
        detail = details[len(details) - 1]
        updatetime = soup.find('div', class_='xg1aie').text


        emaili = soup.find('div', string='Downloads')
        downloads = emaili.parent.find('div', class_='ClM7O').text


        target_mail = detail.text
        print(url, target_mail, downloads, updatetime)
        packagename = url[url.index("=") + 1:]
        try:
            cursor.execute(
                'UPDATE links SET app_update_time = ? WHERE package_name =  ?', (updatetime,packagename))
            conn.commit()
        except:
            pass
    else:
        print(f"Error: {response.status_code}")



for i in range(5204):
    cursor.execute('SELECT link FROM links where id ='+str(i))
    result = cursor.fetchall()
    try:
        getMail(result[0][0])
    except:
        pass


